package cz.proks.easyhelpdesk.gwt.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Widget;

import cz.proks.easyhelpdesk.api.model.User;
import cz.proks.easyhelpdesk.gwt.HelpdeskMessages;
import cz.proks.easyhelpdesk.gwt.ui.composite.RestrictedComposite;
import cz.proks.easyhelpdesk.gwt.ui.layout.IsMainLayoutPanel;
import cz.proks.easyhelpdesk.gwt.ui.layout.MainLayoutPanel;
import cz.proks.easyhelpdesk.gwt.ui.widget.BackButton;
import cz.proks.easyhelpdesk.gwt.ui.widget.H1Widget;
import cz.proks.easyhelpdesk.gwt.ui.widget.UserWidget;

public class UserViewImpl extends RestrictedComposite implements UserView {

    private static UserViewImplUiBinder uiBinder = GWT.create(UserViewImplUiBinder.class);

    interface UserViewImplUiBinder extends UiBinder<Widget, UserViewImpl> {
    }
        
    @UiField HelpdeskMessages i18n;
    @UiField MainLayoutPanel mainPanel;
    @UiField H1Widget header;
    @UiField UserWidget userWidget;
    @UiField BackButton backButton;
    
    private Presenter presenter;

    public UserViewImpl() {
        initWidget(uiBinder.createAndBindUi(this));
    }                
    
    @Override
    protected IsMainLayoutPanel getRootPanel() {
        return mainPanel;
    }

    @Override
    public void setPresenter(Presenter presenter) {
        this.presenter = presenter;
    }
    
    @Override
    public void setUser(User user) {
        userWidget.setId(String.valueOf(user.getId()));
        userWidget.setProviderId(user.getProviderId());
        userWidget.setName(user.getName());
        userWidget.setSurname(user.getSurname());
        userWidget.setEmail(user.getEmail());
        
        header.setText(i18n.user() + " " + user.getName() + " " + user.getSurname());
    }

    @UiHandler("backButton")
    void onBackButtonClick(ClickEvent event) {
        if (presenter != null)
            presenter.back();
    }
}
